home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / graph_alg / _bicomponents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  3.0 KB  |  111 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _bicomponents.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. /*******************************************************************************
  17. *                                                                              *
  18. *  BICONNECTED COMPONENTS                                                      *
  19. *                                                                              *
  20. *  Michael Meiser (1991)                                                       *
  21. *                                                                              *
  22. *******************************************************************************/
  23.  
  24. #include <LEDA/graph_alg.h>
  25. #include <LEDA/stack.h>
  26.  
  27.  
  28.  
  29. static void bcc_dfs(const ugraph& G,node v,edge_array<int>& compnum,
  30.                     node_array<int>& dfsnum,node_array<int>& lowpt,
  31.                     node_array<node>& father,stack<node>& current,
  32.                     int& count1,int& count2);
  33.  
  34.  
  35. int BICONNECTED_COMPONENTS(const ugraph& G, edge_array<int>& compnum)
  36. {
  37.   // computes the biconnected components of an undirected graph G
  38.   // returns m = number of biconnected components
  39.   // returns in edge_array<int> compnum for each edge an integer with
  40.   // compnum[x] = compnum[y] iff edges x and y belong to the same component 
  41.   // and 0 <= compnum[e] <= m-1 for all edges e
  42.   //  running time : O(|V|+|E|)
  43.   //
  44.   // (problem  with self-loops ? )
  45.  
  46.   stack<node> current;
  47.   node_array<int> dfsnum(G,-1);
  48.   node_array<int> lowpt(G,0);
  49.   node_array<node> father(G,nil);
  50.   int count1 = 0; 
  51.   int count2 = 0;
  52.   node v;
  53.  
  54.   forall_nodes(v,G)
  55.    if (dfsnum[v] == -1)
  56.     {
  57.      dfsnum[v] = ++count1;
  58.      current.push(v);
  59.      bcc_dfs(G,v,compnum,dfsnum,lowpt,father,current,count1,count2);
  60.     }
  61.  
  62.   return(count2);
  63.  
  64.  } // BI_COMPONENTS
  65.  
  66.  
  67.  
  68. static void bcc_dfs(const ugraph& G,node v,edge_array<int>& compnum,
  69.                     node_array<int>& dfsnum,node_array<int>& lowpt,
  70.                     node_array<node>& father,stack<node>& current,
  71.                     int& count1,int& count2)
  72.  {
  73.   node w;
  74.  
  75.   lowpt[v] = dfsnum[v];
  76.  
  77.   forall_adj_nodes(w,v)
  78.    if (dfsnum[w] == -1)
  79.     {
  80.      dfsnum[w] = ++count1;
  81.      current.push(w);
  82.      father[w] = v;
  83.      bcc_dfs(G,w,compnum,dfsnum,lowpt,father,current,count1,count2);
  84.      lowpt[v] = Min(lowpt[v],lowpt[w]);
  85.     }
  86.    else
  87.     lowpt[v] = Min(lowpt[v],dfsnum[w]);
  88.  
  89.   if (father[v] && (lowpt[v] == dfsnum[father[v]]))
  90.    // 1.Bedingung nur von Bedeutung fuer Graphen,die nicht zusammen-
  91.    // haengend sind sowie fuer den ersten besuchten Knoten
  92.    {
  93.     edge e;
  94.  
  95.     do
  96.      {
  97.       w = current.pop();
  98.       forall_adj_edges(e,w)
  99.        if (dfsnum[w] > dfsnum[G.opposite(w,e)])
  100.         compnum[e] = count2;
  101.      }
  102.     while (w != v);
  103.  
  104.     count2++;
  105.    }
  106.  
  107.  } // bcc_dfs
  108.  
  109.  
  110.